package com.chenfan.filestore.utils;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xuxianbei
 * Date: 2020/8/31
 * Time: 14:49
 * Version:V1.0
 */
public class PageInfoUtil {


    /**
     * 类型转换
     *
     * @param totalList
     * @param <T>
     * @return
     */
    public static <T> PageInfo<T> toPageInfo(List<T> totalList, Long count) {
        PageInfo<T> resultPageInfo = new PageInfo();
        resultPageInfo.setList(totalList);
        resultPageInfo.setTotal(count);
        return resultPageInfo;
    }

    /**
     * 类型转换
     *
     * @param totalList 从数据库中拿到的list
     * @param volist    返回界面的list
     * @param <T>
     * @return
     */
    public static <T> PageInfo<T> toPageInfo(List<?> totalList, List<T> volist) {
        PageInfo<T> resultPageInfo = new PageInfo(volist);
        PageInfo old = new PageInfo<>(totalList);
        BeanUtils.copyProperties(old, resultPageInfo);
        resultPageInfo.setList(volist);
        resultPageInfo.setTotal(old.getTotal());
        return resultPageInfo;
    }

    /**
     * 类型转换
     *
     * @param old    从数据库中拿到的PageInfo
     * @param volist 返回界面的list
     * @param <T>
     * @return
     */
    public static <T> PageInfo<T> toPageInfo(PageInfo<?> old, List<T> volist) {
        PageInfo<T> resultPageInfo = new PageInfo(volist);
        BeanUtils.copyProperties(old, resultPageInfo);
        resultPageInfo.setList(volist);
        if (CollectionUtils.isEmpty(volist)) {
            resultPageInfo.setTotal(0);
        } else {
            resultPageInfo.setTotal(old.getTotal());
        }
        return resultPageInfo;
    }

    public static <T> PageInfo<T> emptyPageInfo() {
        PageInfo<T> pageInfo = new PageInfo<>();
        pageInfo.setList(new ArrayList<>());
        return pageInfo;
    }
}
